<?php
namespace app\index\controller;
use app\common\controller\Base;
use app\common\model\User as UserModel;
use app\common\model\CarModel;
use think\Session;

class User extends Base
{
	//登录界面
    public function login()
    {
    	$this -> alreadyLogin();
        return $this->view->fetch();
    }
    
	//登录验证
    public function checkLogin(){
    	$status = 0; //验证失败标志
        $result = '验证失败'; //失败提示信息
        $data = $this->request->param();
        
        $rule = [
            'username|姓名' => 'require',
            'password|密码'=>'require',
            // 'verify|验证码' => 'require|captcha'
        ];

        $result = $this->validate($data,$rule);
        if (true === $result) {
        	
        	$map['username'] = $data['username'];
        	$map['password'] = $data['password'];
        	$user = UserModel::withTrashed()->where('username',$data['username'])->where('password',$data['password'])->find();
            // var_dump($user);
            if ($user == null) {
            	# code...
            	$result = '没有该用户或密码错误,请检查';
            }else{
            	$status = 1;
                $result = '验证通过,点击【确定】后进入后台';
                //创建2个session,用来检测用户登陆状态和防止重复登陆
                Session::set('user_id', $user -> id);
                Session::set('user_info', $user -> getData());
            }
    	}

    	return ['status'=>$status, 'message'=>$result, 'data'=>$data];
    }

    public function logout()
    {
    	Session::delete('user_id');
        Session::delete('user_info');

        $this -> success('注销登陆,正在返回',url('user/login'),1);
    }

    
    //管理员列表
    public function adminList()
    {
        $this -> view -> assign('title', '管理员列表');
        $this -> view -> assign('keywords', '乘联会周度数据系统');
        $this -> view -> assign('desc', '乘联会周度数据系统');
        $this -> view -> count = UserModel::count();
        //判断当前是不是admin用户
        //先通过session获取到用户登陆名
        $userName = Session::get('user_info.username');
        if ($userName == 'admin') {
            $list = UserModel::all();
        } else {
            $list = UserModel::where(['username'=>$userName])->select();
        }

        $this -> view -> assign('list', $list);
        //渲染管理员列表模板
        return $this -> view -> fetch('admin_list');
    }
    
    //添加操作的界面
    public function  adminAdd()
    {
        $this->view->assign('title','添加管理员');
        $this->view->assign('keywords','php.cn');
        $this->view->assign('desc','PHP中文网ThinkPHP5开发实战课程');

        $list = CarModel::where("pid",0)->select();
        $this -> view -> assign('list', $list);
        return $this->view->fetch('admin_add');
    }

     //检测用户名是否可用
    public function checkUserName()
    {
        $userName = trim($this->request->param('name'));
        $status = 1;
        $message = '用户名可用';
        if (UserModel::get(['username'=> $userName])) {
            //如果在表中查询到该用户名
            $status = 0;
            $message = '用户名重复,请重新输入~~';
        }
        return ['status'=>$status, 'message'=>$message];
    }

    //添加操作
    public function addUser()
    {
        $data = $this->request-> param();
        $status = 0;
        $message = '添加失败，注册信息不符合要求~~';

        $rule = [
            'username|用户名' => "require|min:3|max:20",
            'password|密码' => "require|min:3|max:20",
            'email|邮箱' => 'require|email'
        ];

        $result = $this -> validate($data, $rule);
        if ($result === true) {
            $userr = UserModel::get(['username' => $data["username"]]);  //判断是否为注册过的用户名称
            $oem = UserModel::get(['oem' => $data["oem"]]);  //判断是否为注册过的厂家
            if ($userr === null && $oem === null) {
                $user= UserModel::create($this->request->param());
                if ($user != null) {
                    $status = 1;
                    $message = '添加成功~~';
                }
            }else{
                $message = '该用户名称或者厂家已经注册~~';
            }
            
        }
        
        return ['status'=>$status, 'message'=>$message];
    }
    
    //编辑用户
    public function adminEdit(){
        $user_id = $this->request -> param('id');
        $result = UserModel::get($user_id);
        $this->view->assign('title','编辑管理员信息');
        $this->view->assign('keywords','php.cn');
        $this->view->assign('desc','PHP中文网ThinkPHP5开发实战课程');
        $this->view->assign('user_info',$result->getData());

        $list = CarModel::where("pid",0)->select();
        $this -> view -> assign('list', $list);
        return $this->view->fetch('admin_edit');
    }

    //更新数据操作
    public function editUser()
    {
        //获取表单返回的数据
//        $data = $request -> param();
        $param = $this->request -> param();
        
        //去掉表单中为空的数据,即没有修改的内容
        foreach ($param as $key => $value ){
            if (!empty($value)){
                $data[$key] = $value;
            }
        }

        $condition = ['id'=>$data['id']] ;
        $result = UserModel::update($data, $condition);

        //如果是admin用户,更新当前session中用户信息user_info中的角色role,供页面调用
        if (Session::get('user_info.name') == 'admin') {
            Session::set('user_info.role', $data['role']);
        }


        if (true == $result) {
            return ['status'=>1, 'message'=>'更新成功'];
        } else {
            return ['status'=>0, 'message'=>'更新失败,请检查'];
        }
    }

    //删除操作
    public function deleteUser()
    {
        $user_id = $this->request -> param('id');
        UserModel::update(['is_delete'=>1],['id'=> $user_id]);
        UserModel::destroy($user_id);

    }

    //删除操作
    public function deleteUserTrue()
    {
        $user_id = $this->request -> param('id');
        // UserModel::update(['is_delete'=>1],['id'=> $user_id]);
        UserModel::destroy($user_id,true);

    }

    //恢复删除操作
    public function unDelete()
    {
        UserModel::update(['delete_time'=>NULL],['is_delete'=>1]);
    }


}
